﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlayBox.DataClasses;

namespace PlayBox
{
    public partial class Shop : UserControl
    {
        public Shop()
        {
            InitializeComponent();

            shopGridView.SelectionChanged += new EventHandler(shopGridView_SelectionChanged);
        }

        void shopGridView_SelectionChanged(object sender, EventArgs e)
        {
            CheckButtonEnabled();
        }

        private void CheckButtonEnabled ()
        {
            bool enabled = false;

            if (User.IsLoggedIn && shopGridView.SelectedRows.Count > 0)
            {
                // Auswahl holen
                Music m = (Music)shopGridView.SelectedRows[0].DataBoundItem;

                // Existiert Datei?
                if (System.IO.File.Exists(User.UserData.GetDirectoryPath("Data\\" + m.ID)))
                    enabled = true;
            }

            btnPlay.Enabled = enabled;
        }

        private void btnGetMusic_Click(object sender, EventArgs e)
        {

            List<Music> music = null;

            try
            {
                music = ServerConnection.Current.GetShopMusic();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Serverabfrage fehlgeschlagen.\n" + ex.Message, "Fehler", MessageBoxButtons.OK);
                return;
            }

            shopGridView.DataSource = music;

            CheckButtonEnabled();
        }

        private void btnBuy_Click(object sender, EventArgs e)
        {
            // Nichts ausgewählt?
            if (shopGridView.SelectedRows.Count == 0)
                return;

            // Auswahl holen
            Music m = (Music)shopGridView.SelectedRows[0].DataBoundItem;

            // Kaufen
            try
            {
                ServerConnection.Current.BuyItem (m.ID);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Serverabfrage fehlgeschlagen.\n" + ex.Message, "Fehler", MessageBoxButtons.OK);
                return;
            }

            // Erfolgreich
            MessageBox.Show("Erfolgreich gekauft!", "Erfolgreich", MessageBoxButtons.OK);
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            // Nichts ausgewählt?
            if (shopGridView.SelectedRows.Count == 0)
                return;

            // Auswahl holen
            Music m = (Music)shopGridView.SelectedRows[0].DataBoundItem;

            // Download
            var download = new Download();
            download.music = m;
            
            download.StartDownload (m.ID, m.ID);


            CheckButtonEnabled();
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
             // Nichts ausgewählt?
            if (shopGridView.SelectedRows.Count == 0)
                return;

            // Auswahl holen
            Music m = (Music)shopGridView.SelectedRows[0].DataBoundItem;

            m.InitFromDisk(m.ID);
            m.Play();
        }
    }
}
